//
//  VowShopListViewController.m
//  ZiJinLian
//
//  Created by 🐛 on 2017/11/11.
//  Copyright © 2017年 lzy. All rights reserved.
//

#import "VowShopListViewController.h"
#import "VowCollectionViewCell.h"
#import "ShopDetailViewController.h"
#import "ClassModle.h"
#import "VowViewController.h"
#import "MyWishViewController.h"
@interface VowShopListViewController ()<UICollectionViewDelegate,UICollectionViewDataSource,UICollectionViewDelegateFlowLayout,UISearchBarDelegate>
@property (strong, nonatomic) UICollectionView      *collectionView;
@property (nonatomic, strong) ClassModle * shopModle;
@property (nonatomic, strong) NSMutableArray        * menuMainArray;
@property (nonatomic, strong) VowCollectionViewCell        * cell;
@property (nonatomic, strong) NSDictionary        * dic;
@property (nonatomic, assign) int       pageNo;
@end

@implementation VowShopListViewController
-(NSMutableArray*)menuMainArray{
    if (!_menuMainArray) {
        _menuMainArray=[NSMutableArray array];
    }
    return _menuMainArray;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.title=@"选择佛像";
    self.pageNo=1;
    [self.view addSubview:self.collectionView];
    [self showTempView:@"暂无此类佛像" Image:@"img_fail_monk" CGFrame:CGRectMake(0, 0, MainW,MainH-64-49)];
    self.tempView.hidden=YES;
    UIButton*determineBtn=[UIButton buttonWithType:UIButtonTypeSystem];
    determineBtn.frame=CGRectMake(0, MainH-49-64, MainW , 49);
    [determineBtn setTitle:@"确定" forState:0];
    [determineBtn setTitleColor:[UIColor whiteColor] forState:0];
    determineBtn.backgroundColor=Red_Color;
    [determineBtn addTarget:self action:@selector(userprayaddClick) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:determineBtn];
    if (self.isMatch==YES) {
        [self loadSearchData];
    }else{
        [self loadSearchDatalist];
    }
    
    // Do any additional setup after loading the view.
}

#pragma mark --- Getter
- (UICollectionView *)collectionView
{
    if (!_collectionView) {
        UICollectionViewFlowLayout *flowlayout = [[UICollectionViewFlowLayout alloc] init];
        //设置滚动方向
        [flowlayout setScrollDirection:UICollectionViewScrollDirectionVertical];
        //左右间距
        flowlayout.minimumInteritemSpacing =5;
        //上下间距
        flowlayout.minimumLineSpacing =5 ;
        _collectionView = [[UICollectionView alloc]initWithFrame:CGRectMake(0, 0, MainW, MainH-64-50) collectionViewLayout:flowlayout];
        _collectionView.backgroundColor = Gray_Color;
        _collectionView.delegate = self;
        _collectionView.dataSource = self;
        _collectionView.showsVerticalScrollIndicator = NO;
        _collectionView.showsHorizontalScrollIndicator = NO;
        
        [_collectionView registerNib:[UINib nibWithNibName:@"VowCollectionViewCell" bundle:[NSBundle mainBundle]] forCellWithReuseIdentifier:@"ShopsCell"];
        _collectionView.mj_footer=[MJRefreshBackStateFooter footerWithRefreshingBlock:^{
            self.pageNo++;
            if (self.isMatch==YES) {
                [self loadSearchData];
            }else{
                [self loadSearchDatalist];
            }
            [_collectionView.mj_footer endRefreshing];
        }];
        
    }
    return _collectionView;
}
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    return 1;
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return self.menuMainArray.count;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    self.shopModle = self.menuMainArray[indexPath.row];
    VowCollectionViewCell * cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"ShopsCell" forIndexPath:indexPath];
    [cell.headImg sd_setImageWithURL:[NSURL URLWithString:self.shopModle.imageUrl] placeholderImage:[UIImage imageNamed:@"placeholderImage"]];
    cell.titleLabel.text=self.shopModle.title;
    cell.selectBtn.tag=indexPath.row;
    cell.selectBtn.selected=NO;
    [cell.selectBtn addTarget:self action:@selector(chooseShopClick:) forControlEvents:UIControlEventTouchUpInside];
    return cell;
}
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    self.shopModle = self.menuMainArray[indexPath.row];
    ShopDetailViewController * VC = [[ShopDetailViewController alloc] init];
    VC.spuId=self.shopModle.skuId;
    [self.navigationController pushViewController:VC animated:YES];
}
#pragma mark - UICollectionViewDelegateFlowLayout
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
    return CGSizeMake((MainW-15)/2, (MainW-15)/2+30);
}
-(UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section
{
    return UIEdgeInsetsMake(5, 5, 5, 5);//分别为上、左、下、右
}

#pragma mark -------------- 筛选列表api
-(void)loadSearchData{
    NSMutableDictionary*dic=[[NSMutableDictionary alloc]init];
    dic[@"cateId"]=self.ids;
    dic[@"userId"]=[ZJUserDefaultsTools getCurrentUserID];;
    dic[@"pageNumber"]=@(self.pageNo);
    dic[@"pageSize"]=@"10";
    [self doHttpRequestWithApi:prayBuddhaSku method:GET dic:dic successBlock:^(NSDictionary *dic) {
        [self.menuMainArray addObjectsFromArray:[ClassModle mj_objectArrayWithKeyValuesArray:dic[@"data"][@"list"]]];
        if (self.menuMainArray.count!=0) {
            self.tempView.hidden=YES;
        }else{
            self.tempView.hidden=NO;
        }
        [self.collectionView reloadData];
        
    }];
}

-(void)loadSearchDatalist{
    NSMutableDictionary*dic=[[NSMutableDictionary alloc]init];
    dic[@"cateId"]=self.ids;
    dic[@"pageSize"]=@(10);
    dic[@"pageNumber"]=@(self.pageNo);
    [self doHttpRequestWithApi:API_SearchCate method:POST dic:dic successBlock:^(NSDictionary *dic) {
        [self.menuMainArray addObjectsFromArray:[ClassModle mj_objectArrayWithKeyValuesArray:dic[@"data"][@"list"]]];
        if (self.menuMainArray.count!=0) {
            self.tempView.hidden=YES;
        }else{
            self.tempView.hidden=NO;
        }
        [self.collectionView reloadData];
        
    }];
}


-(void)chooseShopClick:(UIButton *)sender{
    sender.selected=!sender.selected;
    if (sender.selected) {
        [sender setImage:[UIImage imageNamed:@"icon_rb_select"] forState:0];
    }else{
        [sender setImage:[UIImage imageNamed:@"icon_rb_nor"] forState:0];
    }
    VowCollectionViewCell*cell=(VowCollectionViewCell*)[[sender superview] superview];
    if (self.cell==nil) {
        self.cell=cell;
    }else if(self.cell!=cell){
        self.cell.selectBtn.selected=NO;
        [self.cell.selectBtn setImage:[UIImage imageNamed:@"icon_rb_nor"] forState:0];
        self.cell=cell;
    }
    self.shopModle = self.menuMainArray[sender.tag];
    
    
}
-(void)userprayaddClick{
    if (self.popWhere==1) {
        MyWishViewController *viewCtl = self.navigationController.viewControllers[self.popWhere];
        viewCtl.shopModle=self.shopModle;
        viewCtl.shopModle.isMatch=self.isMatch;
        [self.navigationController popToViewController:viewCtl animated:YES];
    }else{
        VowViewController *viewCtl = self.navigationController.viewControllers[self.popWhere];
        viewCtl.shopModle=self.shopModle;
        viewCtl.shopModle.isMatch=self.isMatch;
        [self.navigationController popToViewController:viewCtl animated:YES];
    }
    
}
@end
